<template>
  <div class="mh-80vh w-full flex">
    <!-- 第一步，通过流程定义的列表，选择对应的流程 -->
    <ContentWrap class="mr-10px w-40%">
      <div class="grid grid-cols-4 grid-gap-10px" style="grid-template-rows: 100px">
        <div
          @click="handleSelect(item)"
          v-for="item in list"
          :key="item.id"
          class="btn h-full w-full flex flex-col cursor-pointer items-center justify-center bg-#f0f0f0"
        >
          <el-icon size="38"><Notification /></el-icon>
          <div class="mt-9px text-14px">{{ item.name }}</div>
        </div>
      </div>
    </ContentWrap>

    <!-- 第二步，填写表单，进行流程的提交 -->
    <ContentWrap class="w-60%">
      <el-card class="box-card" v-if="selectProcessInstance">
        <div class="clearfix">
          <span class="el-icon-document">申请信息【{{ selectProcessInstance.name }}】</span>
        </div>
        <el-col :span="16" :offset="6" style="margin-top: 20px">
          <form-create
            :rule="detailForm.rule"
            v-model:api="fApi"
            :option="detailForm.option"
            @submit="submitForm"
          />
        </el-col>
      </el-card>
      <!-- 流程图预览 -->
      <ProcessInstanceBpmnViewer v-if="selectProcessInstance" :bpmn-xml="bpmnXML as any" />

      <el-empty v-if="!selectProcessInstance" description="请选择审批" />
    </ContentWrap>
  </div>
</template>
<script lang="ts" setup>
import { Notification } from '@element-plus/icons-vue'
import { DICT_TYPE } from '@/utils/dict'
import * as DefinitionApi from '@/api/bpm/definition'
import * as ProcessInstanceApi from '@/api/bpm/processInstance'
import { setConfAndFields2 } from '@/utils/formCreate'
import type { ApiAttrs } from '@form-create/element-ui/types/config'
import ProcessInstanceBpmnViewer from '../detail/ProcessInstanceBpmnViewer.vue'

defineOptions({ name: 'BpmProcessInstanceCreate' })

const router = useRouter() // 路由
const message = useMessage() // 消息

// ========== 列表相关 ==========
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const queryParams = reactive({
  suspensionState: 1
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    list.value = await DefinitionApi.getProcessDefinitionList(queryParams)
  } finally {
    loading.value = false
  }
}

// ========== 表单相关 ==========
const bpmnXML = ref(null) // BPMN 数据
const fApi = ref<ApiAttrs>()
const detailForm = ref({
  // 流程表单详情
  rule: [],
  option: {}
})
const selectProcessInstance = ref() // 选择的流程实例

/** 处理选择流程的按钮操作 **/
const handleSelect = async (row) => {
  // 设置选择的流程
  selectProcessInstance.value = row

  // 情况一：流程表单
  if (row.formType == 10) {
    // 设置表单
    setConfAndFields2(detailForm, row.formConf, row.formFields)
    // 加载流程图
    bpmnXML.value = await DefinitionApi.getProcessDefinitionBpmnXML(row.id)
    // 情况二：业务表单
  } else if (row.formCustomCreatePath) {
    await router.push({
      path: row.formCustomCreatePath
    })
    // 这里暂时无需加载流程图，因为跳出到另外个 Tab；
  }
}

/** 提交按钮 */
const submitForm = async (formData) => {
  if (!fApi.value || !selectProcessInstance.value) {
    return
  }
  // 提交请求
  fApi.value.btn.loading(true)
  try {
    await ProcessInstanceApi.createProcessInstance({
      processDefinitionId: selectProcessInstance.value.id,
      variables: formData
    })
    // 提示
    message.success('发起流程成功')
    router.go(-1)
  } finally {
    fApi.value.btn.loading(false)
  }
}

/** 初始化 */
onMounted(() => {
  getList()
})
</script>
<style lang="scss" scope>
* {
  box-sizing: content-box;
}
.btn {
  background: none;
  text-transform: uppercase;
  color: #000;
  font-weight: bold;
  position: relative;
  outline: none;
  padding: 26px 0px;
  box-sizing: border-box;
  border: 1px solid #ddd;
}

.btn::before,
.btn::after {
  box-sizing: inherit;
  position: absolute;
  content: '';
  border: 2px solid transparent;
  width: 0;
  height: 0;
}

.btn::after {
  bottom: 0;
  right: 0;
}

.btn::before {
  top: 0;
  left: 0;
}
.btn:hover {
  color: #0052cc;
}
.btn:hover::before,
.btn:hover::after {
  width: 100%;
  height: 100%;
}

.btn:hover::before {
  border-top-color: #0052cc;
  border-right-color: #0052cc;
  transition:
    width 0.1s ease-out,
    height 0.1s ease-out 0.1s;
}

.btn:hover::after {
  border-bottom-color: #0052cc;
  border-left-color: #0052cc;
  transition:
    border-color 0s ease-out 0.2s,
    width 0.1s ease-out 0.2s,
    height 0.1s ease-out 0.3s;
}
</style>
